<template>
  <div @keyup.enter="getData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" placeholder="主体名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" placeholder="负责人查询" :clearable="true"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:customer="scope">
        <show-project-attrib :data="scope.row"></show-project-attrib>
      </template>
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:itemStatus="scope">
        <span class="badge" :class="`badge-${scope.row.itemStatusStyle}`">
          {{ scope.row.itemStatusLabel }}
        </span>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="active">
        <el-tab-pane label="项目信息" name="first">
          <info-view :related-data="relatedData"></info-view>
        </el-tab-pane>
        <el-tab-pane label="合同审批表">
          <contract-approve
            :query-id="queryId"
            :process-status="relatedData.process_status"
            view-mode
          ></contract-approve>
        </el-tab-pane>
        <el-tab-pane label="项目审批表">
          <investigate-approve-view
            :query-id="queryId"
            :process-status="relatedData.process_status"
          ></investigate-approve-view>
        </el-tab-pane>
        <el-tab-pane label="合同信息" name="contract">
          <contract-guard-view :query-id="queryId"></contract-guard-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter" v-if="+relatedData.ele_letter_status > 0">
          <letter-view :query-id="queryId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :query-id="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>
    <contract-approve-res
      :visible.sync="editVisible"
      @success="handleSuccess"
      :related-data="editData"
    ></contract-approve-res>
    <contract-approve-dialog
      :visible.sync="approvalVisible"
      @success="handleSuccess"
      :query-id="editQueryId"
    ></contract-approve-dialog>
  </div>
</template>

<script>
import { enumGuatype, enumProjectResStatus, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/city/bus/normal/item/List'
import baseMixins from '@/credit/views/city/bus/normal/todoMixins'

import SelectAddress from '@/components/select/SelectAddress'
import AttachView from '@/credit/components/AttachView'
import AttachCmpt from '@/credit/views/city/program/Attach'

import InvestigateApproveView from '@/credit/views/county/unionProvince/components/InvestigateApproveView'
import ContractApproveRes from '@/credit/views/county/unionProvince/components/contractApproveDeploy/BaseDialog'
import ContractApproveDialog from '@/credit/views/county/unionProvince/components/contractApproveDeploy/MainDialog'
import ContractGuardView from '@/credit/views/county/unionProvince/components/ContractGuardView'
import ContractApprove from '@/credit/views/county/unionProvince/components/contractApproveDeploy/Main'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import InfoView from '@/credit/views/city/bus/normal/Info'
import ShowProjectAttrib from '@/credit/views/city/program/new/ShowProjectAttrib'

export default {
  components: {
    SelectAddress,
    AttachView,
    AttachCmpt,
    InvestigateApproveView,
    ContractApproveRes,
    ContractApproveDialog,
    ContractGuardView,
    ContractApprove,
    LetterView,
    InfoView,
    ShowProjectAttrib,
  },
  mixins: [baseMixins],
  data() {
    return {
      enumProjectResStatus,
      order: {
        approved_date: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
        'task_type.in': '8,35',
        subKey: 'project_approval_register.customer_related_id',
      },
      tableData: [],
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '审批金额(万元)', prop: 'approveFunds', align: 'center' } },
        { attrs: { label: '审批完成时间', prop: 'approveTime', width: 140 } },
        { attrs: { label: '担保机构', prop: 'service_name', align: 'center', minWidth: '110px' } },
        {
          attrs: { label: '合作银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        { attrs: { label: '业务模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '区县项目经理' }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', width: 140 },
          operations: [
            {
              content: '合同处理',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              // ifRender: row => row.editable,
            },
            // { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      editRow: {},
      editVisible: false,
      editQueryId: '',
      approvalVisible: false,
      reportVisible: false,
      relatedData: {},
      attachVisible: false,
      attachRelatedId: '',
      queryId: '',
      active: 'first',
      editData: {},
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getData()
  },
  methods: {
    handleSuccess() {
      this.getData(this.params.index)
      this.$store.dispatch('getTaskCountBusUnion')
    },
    handleEdit(row = {}) {
      this.editData = { ...row }
      this.editQueryId = row.customer_related_id || ''
      if (row.res_a === this.userId) {
        this.editVisible = true
      } else {
        this.approvalVisible = true
      }
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.approveTime = this.$utils.dateFormat(
        item.city_res_date_corp || item.res_date_corp,
        this.dateFmt
      )

      const projectApproval = item.project_approval_register || []
      const status = +item.process_status ? 2 : 1 // 权限外用省农担秘书登记权限内用区县秘书
      const approve = projectApproval.find(item => +item.approval_register_type === status) || {}
      item.approveFunds = approve.res_funds || ''

      const itemStatus = enumProjectResStatus[item.project_res_status] || {}
      item.itemStatusLabel = itemStatus.label
      item.itemStatusStyle = itemStatus.css
      item.processLabel = enumProcessStatus[+item.process_status] || ''

      item.editable = true // item.res_a === this.userId

      return item
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },
    handleView(row) {
      this.active = 'first'
      this.queryId = row.customer_related_id
      this.relatedData = row
      this.reportVisible = true
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
